package com.abaaba.project2.controller.wxcontroller;

import com.abaaba.project2.bean.OrderGoods;
import com.abaaba.project2.bean.bo.OrderSubmitBo;
import com.abaaba.project2.bean.bo.WxOrderCommentsBo;
import com.abaaba.project2.bean.bo.WxOrderListBo;
import com.abaaba.project2.bean.vo.BaseRespVo;
import com.abaaba.project2.bean.vo.WxOrderDetailVo;
import com.abaaba.project2.bean.vo.WxOrderListVo;
import com.abaaba.project2.service.WxOrderService;
import com.abaaba.project2.util.UserIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService wxOrderService;


    @RequestMapping("/list")
    public BaseRespVo list(WxOrderListBo wxOrderListBo) {

        WxOrderListVo wxOrderListVo = null;
        try {
            wxOrderListVo = wxOrderService.queryOrderListByStatus(wxOrderListBo,UserIdUtil.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok(wxOrderListVo);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId) {
        WxOrderDetailVo wxOrderDetailVo = null;
        try {
            wxOrderDetailVo = wxOrderService.queryOrderDetail(orderId);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.fail();
        }

        return BaseRespVo.ok(wxOrderDetailVo);
    }


    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody Map order) {
        try {
            wxOrderService.updateOrderStatus(((Integer)order.get("orderId")),Short.parseShort("102"),UserIdUtil.getUserId());

        }catch (Exception e){
            e.printStackTrace();
            return  BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map order) {
        try {
            wxOrderService.updateOrderStatus(((Integer)order.get("orderId")),Short.parseShort("203"),UserIdUtil.getUserId());
        }catch (Exception e){
            e.printStackTrace();
            return  BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }


    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map order) {
        try {
            wxOrderService.updateOrderDeleted(((Integer)order.get("orderId")),UserIdUtil.getUserId());
        }catch (Exception e){
            e.printStackTrace();
            return  BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map order) {
        try {
            wxOrderService.updateOrderStatus(((Integer)order.get("orderId")),Short.parseShort("401"),UserIdUtil.getUserId());
        }catch (Exception e){
            e.printStackTrace();
            return  BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody WxOrderCommentsBo wxOrderCommentsBo) {

        try {
            wxOrderService.insertComments(wxOrderCommentsBo,UserIdUtil.getUserId());
        }catch (Exception e){
            e.printStackTrace();
            return  BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("goods")
    public BaseRespVo goods(Integer orderId, Integer goodsId) {
        OrderGoods orderGoods;
        try {
            orderGoods = wxOrderService.queryOrderGoods(orderId, goodsId);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.fail();
        }

        return BaseRespVo.ok(orderGoods);
    }

    @RequestMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map order) {

        //模拟重新支付
        return BaseRespVo.fail(724,"订单不能支付");
    }

    @RequestMapping("submit")
    public BaseRespVo submit(@RequestBody OrderSubmitBo orderSubmitBo) {

//        //模拟提交订单
//        Integer orderId = null;
//
//        try {
//            orderId = wxOrderService.insertOrder(orderSubmitBo,UserIdUtil.getUserId());
//        } catch (Exception e) {
            return BaseRespVo.fail(724, "失败");
//        }
//
//        return BaseRespVo.ok(orderId);
    }

}
